require 'date'

def calendarTable(month=2,year=2007)
  first, last = Date.new(year,month,1), Date.new(year,month+1,1)-1
  daysOfMonth = last - first + 1
  monthSet = (1..(first-1).wday).collect{0} +
             (1..daysOfMonth).to_a +
             (1..(Date::DAYNAMES.length - last.wday)).collect{0}
  monthSet.collect!{|x| x==0? " "*7:"%02d {o} "%x }
  weeks=(0...monthSet.length/7).collect{|i|
        "||#{i+1}주||#{monthSet[i*7...i*7+7].join('||')}||"}

  head=%Q[||<-8 rowstyle="text-align:center">#{year}년 #{month}월||]
  weekHead="||   ||     월||     화||     수||     목||     금||     토||     일||"
  [head,weekHead,weeks.join("\n")].join("\n")
end

puts calendarTable(2)
